package br.unifor.cct.mia.util;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import br.unifor.cct.mia.dataenhancement.Attribute;
import br.unifor.cct.mia.dataenhancement.Database;
import br.unifor.cct.mia.dataenhancement.Structure;

public class LoadFile {
	
	public static Structure loadStructure(String pathFile) {
		Structure structure = new Structure();
		structure.setStrucFile(pathFile);
		
		try {
			BufferedReader br = new BufferedReader(new FileReader(pathFile));
			
			try {
				String line = br.readLine();
				double max = 0, min = 0;
				boolean isInteger = false;
				
				while (line != null) {
					/*                 0      1     2...
					  Discreto => nomeColuna Tipo Valores
					  				   0	  1	    2     3   4
					  Continuo => nomeColuna Tipo Inteiro Min Max
					  
					  Inteiro => true ou false;
					  Tipo => discrete ou continuous*/
					Attribute attribute = null;
					List values = new ArrayList();
					String[] bounds = line.split(" ");
				
					if (bounds[1].equalsIgnoreCase(Attribute.CONTINUOUS)) {
						isInteger = Boolean.valueOf(bounds[2]).booleanValue();
						
						if ( bounds.length>3 ) {
							min = Double.parseDouble(bounds[3]);
							max = Double.parseDouble(bounds[4]); 
	
							values.add(new Double(min));
							values.add(new Double(max));
						}
						else {
							min = 0;
							max = Integer.MAX_VALUE;
						}
						
						attribute = new Attribute(bounds[0], Attribute.CONTINUOUS, min, max, isInteger);
					}
					else {
						for (int i = 2; i < bounds.length; i++) values.add(bounds[i].toLowerCase());						
						attribute = new Attribute(bounds[0], Attribute.DISCRETE, 0, bounds.length-3, true);
					}

					structure.addElement(attribute, values);
					line = br.readLine();
				}			
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return structure;
	}	

	public static Database loadDatabase(String pathFile) {
		Database db = new Database();
		db.setDbFile(pathFile);
		
		try {
			BufferedReader br = new BufferedReader(new FileReader(pathFile));
			String line;
			try {
				line = br.readLine();
				while (line != null) {
					String[] bounds = line.toLowerCase().split(",");
					db.add(bounds);					
					line = br.readLine();
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}		
		
		return db;
	}

	
	public static List generateFaultDatabase(int tamLinha, int tamCol, int hidden) {
		List positions = new ArrayList();
		int i, j = 0;
		
		while (hidden > 0) {
			i = Methods.randIntValue(0, tamLinha-1);
			j = Methods.randIntValue(0, tamCol-1);
	
			Point p = new Point(i, j); 
			if (!positions.contains(p)) {
				positions.add(p);
				hidden--;
			}
		}
		
		return positions;
	}
	
	public static List getFaultDatabase(Structure st, Database db) {
		List positions = new ArrayList();
		
		for ( int i=0; i<db.size(); i++ ) {
			String[] linha = db.getValues(i);
			for ( int j=0; j<st.size(); j++ ) {
				String value = linha[j];
				if ( value.equals("?") ) {
					Point p = new Point(i, j); 
					positions.add(p);
				}
			}
		}		
		return positions;
	}	
}